import {
  getSysBannerTable,
  editSysBannerRecord,
  deleteSysBannerRecord
} from '../../api/operate'
import {
  SYS_BANNER_LIST,
  SYS_BANNER_CURRENT_ROW,
  SYS_BANNER_DATA_COUNT,
  SYS_BANNER_HISTORY_DATA,
  SYS_BANNER_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    sysBannerList: [],
    sysBannerDataCount: null,
    sysBannerPageSize: 20,
    sysBannerHistoryData: [],
    row: {
      url: '',
      title: '',
      taskid: '',
      time: '',
      begintime: '',
      endtime: '',
      sort: '',
      state: '',
      openState: false
    }
  },
  getters: {
    sysBannerList: state => state.sysBannerList
  },
  mutations: {
    [SYS_BANNER_LIST] (state, data) {
      state.sysBannerList = data
    },
    [SYS_BANNER_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [SYS_BANNER_DATA_COUNT] (state, data) {
      state.sysBannerDataCount = data
    },
    [SYS_BANNER_HISTORY_DATA] (state, data) {
      state.sysBannerHistoryData = data
    },
    [SYS_BANNER_PAGE_SIZE] (state, data) {
      state.sysBannerPageSize = data
    }
  },
  actions: {
    async getSysBannerList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSysBannerTable(data)
      // console.log(resp)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          host: host,
          url: item.url ? item.url : '',
          title: item.title,
          taskid: item.taskid,
          begintime: item.begintime,
          endtime: item.endtime,
          time: [item.begintime, item.endtime],
          sort: item.sort.toString(),
          state: item.state,
          openState: item.state === 0
        }]
      })
      commit(SYS_BANNER_LIST, newData)
      commit(SYS_BANNER_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.sysBanner.sysBannerPageSize) {
        commit(SYS_BANNER_HISTORY_DATA, newData)
      } else {
        commit(SYS_BANNER_HISTORY_DATA, newData.slice(0, rootState.sysBanner.sysBannerPageSize))
      }
      return resp
    },
    async editSysBannerData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editSysBannerRecord(data)
      return resp
    },
    async deleteSysBannerData ({ commit, rootState }, playload) {
      const resp = await deleteSysBannerRecord(playload)
      return resp
    },
    async getSysBannerCurrentRow ({ commit, rootState }, row) {
      commit(SYS_BANNER_CURRENT_ROW, row)
    },
    async changeSysBannerPage ({ commit, rootState }, index) {
      let data = rootState.sysBanner.sysBannerList
      let _start = (index - 1) * rootState.sysBanner.sysBannerPageSize
      let _end = index * rootState.sysBanner.sysBannerPageSize
      commit(SYS_BANNER_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeSysBannerPageSize ({ commit, rootState }, value) {
      commit(SYS_BANNER_PAGE_SIZE, value)
    }
  }
}
